#pragma once

#include <mysql/mysql.h>
#include <jsoncpp/json/json.h>
#include <vector>
#include <string>
#include <iostream>
#include <mutex>

#define HOST "150.158.42.139"
#define DB "chatsystem"
#define DBPORT 3306
#define USER "cdd"
#define PASSWD "123456"

using std::string;

class DataBaseSvr {
public:
  DataBaseSvr() {
    mysql_ = NULL;
  }
  
  //获取所有用户信息
  bool GetAllUser(Json::Value* alluser) {
#define GETALLUSER "select * from user;"
    lock_.lock();
    //1.查询
    if (MysqlQuery(GETALLUSER) == false) {
      return false;
    }
    //获取结果集
    MYSQL_RES* res = mysql_store_result(mysql_);
    if (res == NULL) {
      return false;
    }
    lock_.unlock();
    //获取行数
    int rows = mysql_num_rows(res);
    for (int i = 0; i < rows; i++) {
      MYSQL_ROW row = mysql_fetch_row(res);
      //组织数据,方便返回
      Json::Value tem;
      tem["userid"] = atoi(row[0]);
      tem["nickname"] = row[1];
      tem["school"] = row[2];
      tem["telnum"] = row[3];
      tem["passwd"] = row[4];
      alluser->append(tem);
    }

    mysql_free_result(res);
    return true;

  }
  
  //初始化接口
  //连接mysql服务器
  //设置字符集
  bool MysqlInit() {
    mysql_ = mysql_init(NULL);
    if (mysql_ == NULL) {
      std::cout << "mysql init failed" << std::endl;
      return false;
    }
    if (mysql_real_connect(mysql_, HOST, USER, PASSWD, DB, DBPORT, NULL, 0) == NULL) {
      std::cout << "mysql connect failed" << std::endl;
      mysql_close(mysql_);
      return false;
    }
    mysql_set_character_set(mysql_, "utf8");
    return true;
  }

  //获取某个人的好友列表
  bool GetFriend(int userid, std::vector<int>* f_id) {
#define GETFRIEND "select friend from friendinfo where useid='%d';"
    
    //创建sql语句
    char sql[1024] = {0};
    sprintf(sql, GETFRIEND, userid);
    lock_.lock();
    //查询
    if (MysqlQuery(sql) == false) {
      return false;
    }

    //获取结果集
    MYSQL_RES* res = mysql_store_result(mysql_);
    if (res == NULL) {
      return false;
    }
    lock_.unlock();
    //获取行数
    int rows = mysql_num_rows(res);
    for (int i = 0; i < rows; i++) {
      MYSQL_ROW row = mysql_fetch_row(res);
      f_id->push_back(atoi(row[0]));    
    }

    mysql_free_result(res);
    return true;
        
  }

  //添加用户
  bool InsertUser(int userid, const string& nickname, const string& school, const string& telnum, const string& passwd) {
#define INSERTUSER "insert into user(userid, nickname, school, telnum, passwd) values('%d', '%s', '%s', '%s', '%s');"

    //创建sql语句
    char sql[1024] = {0};
    sprintf(sql, INSERTUSER, userid, nickname.c_str(), school.c_str(), telnum.c_str(), passwd.c_str());

  //插入
    if (MysqlQuery(sql) == false) {
      return false;
    }
    return true;

  }
  
  //添加好友
  bool InsertFriend(int userid1, int userid2) {
#define INSERTFRIEND "insert into friendinfo values('%d', '%d');"

    //创建sql语句
    char sql[1024] = {0};
    sprintf(sql, INSERTFRIEND, userid1, userid2);

    //插入
    if (MysqlQuery(sql) == false) {
      return false;
    }
    return true;
    
  }

  ~DataBaseSvr() {
    if (mysql_ != NULL) {
      mysql_close(mysql_);
    }
  }

private:

  bool MysqlQuery(const string& sql) {
    if (mysql_query(mysql_, sql.c_str()) != 0) {
      std::cout << "mysql exec failed" << std::endl;
      return false;
    }
    return true;
  }
private:
  MYSQL* mysql_;
  std::mutex lock_;
};
